/*
 * @Author: zhangguowei 974599791@qq.com
 * @Date: 2022-09-27 10:48:54
 * @LastEditors: zhangguowei 974599791@qq.com
 * @LastEditTime: 2022-10-11 17:33:00
 * @Description: 页面布局相关store
 */
import { defineStore } from "pinia";
interface tag {
  path: string;
  label: string;
  checked: boolean; //是否被激活
}
interface layoutStore {
  isCollapse: boolean;
  tagList: tag[];
  active: string;
  breadcrumb: string[];
}

export const useLayoutStore = defineStore({
  id: "layout",
  state: (): layoutStore => {
    return {
      isCollapse: false, // 侧边栏折叠状态控制
      tagList: [], // tag打开列表
      active: null, // 当前激活的路由(是一个路径)
      breadcrumb: [], // 面包屑会用到
    };
  },
  getters: {
    getisCollapse(): boolean {
      return this.isCollapse;
    },
  },
  actions: {
    setbreadcrumb(val: string[]) {
      this.breadcrumb = val;
    },
    setisCollapse(val: boolean) {
      this.isCollapse = val;
    },
    addtagList(val: tag) {
      // 统一关闭所有激活状态
      this.tagList.forEach((item) => {
        item.checked = false;
      });
      let find = this.tagList.find((item) => item.path == val.path);
      // 不允许重复添加
      if (!find) {
        this.tagList.push(Object.assign({}, val, { checked: true }));
      }
      // 设置激活状态
      else {
        find.checked = true;
      }
    },
    setactive(val: string) {
      this.active = val;
    },
  },
});
